<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>网络分析服务-最佳路径分析</title>

    <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="http://cdn.bootcss.com/openlayers/4.2.0/ol.css" rel="stylesheet">
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%">
<div id="map" style="width: 100%;height:100%"></div>
<div id="stopMarker1">
    <img src="http://support.supermap.com.cn:8090/iserver/iClient/forJavaScript/theme/images/marker.png"/>
</div>
<div id="stopMarker2">
    <img src="http://support.supermap.com.cn:8090/iserver/iClient/forJavaScript/theme/images/marker.png"/>
</div>
<div id="stopMarker3">
    <img src="http://support.supermap.com.cn:8090/iserver/iClient/forJavaScript/theme/images/marker.png"/>
</div>

<script src="http://cdn.bootcss.com/openlayers/4.2.0/ol.js"></script>
<script type="text/javascript" src="../../dist/iclient9-openlayers.min.js"></script>
<script type="text/javascript">
    var map,
        baseUrl = "http://support.supermap.com.cn:8090/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = "http://support.supermap.com.cn:8090/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";
    //最佳路径分析是按照站点添加顺序对站点进行访问的
    var extent = [48.4, -7668.25, 8958.85, -55.58];
    var projection = new ol.proj.Projection({
        extent: extent,
        units: 'm'
    });
    new ol.supermap.MapService(baseUrl).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            controls: ol.control.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo()]),
            view: new ol.View({
                center: [5200, -3375],
                zoom: 3,
                projection: projection
            })
        });
        var options = ol.source.TileSuperMapRest.optionsFromMapJSON(baseUrl, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        map.addLayer(layer);
        findPathProcess();
    });

    function findPathProcess() {
        //添加站点
        var stopMarker1 = new ol.Overlay({
            element: document.getElementById('stopMarker1'),
            positioning: 'center-center',
            position: [4000, -3000]
        });
        var stopMarker2 = new ol.Overlay({
            element: document.getElementById('stopMarker2'),
            positioning: 'center-center',
            position: [5500, -2500]
        });
        var stopMarker3 = new ol.Overlay({
            element: document.getElementById('stopMarker3'),
            positioning: 'center-center',
            position: [6900, -4000]
        });
        map.addOverlay(stopMarker1);
        map.addOverlay(stopMarker2);
        map.addOverlay(stopMarker3);

        //创建最佳路径分析参数实例
        var resultSetting = new SuperMap.TransportationAnalystResultSetting({
            returnEdgeFeatures: true,
            returnEdgeGeometry: true,
            returnEdgeIDs: true,
            returnNodeFeatures: true,
            returnNodeGeometry: true,
            returnNodeIDs: true,
            returnPathGuides: true,
            returnRoutes: true
        });
        var analystParameter = new SuperMap.TransportationAnalystParameter({
            resultSetting: resultSetting,
            weightFieldName: "length"
        });
        var findPathParameter = new SuperMap.FindPathParameters({
            isAnalyzeById: false,
            nodes: [new ol.geom.Point([4000, -3000]), new ol.geom.Point([5500, -2500]), new ol.geom.Point([6900, -4000])],
            hasLeastEdgeCount: false,
            parameter: analystParameter
        });

        //进行查找
        new ol.supermap.NetworkAnalystService(serviceUrl).findPath(findPathParameter, function (serviceResult) {
            var guideLayerStyle = new ol.style.Style({
                image: new ol.style.Icon(({
                    src: 'http://support.supermap.com.cn:8090/iserver/iClient/forJavaScript/examples/images/walk.png',
                    size: [20, 20]
                }))
            });
            var routeLayerStyle = new ol.style.Style({
                stroke: new ol.style.Stroke({
                    color: 'rgba(100, 100, 225, 10)',
                    width: 3
                }),
                fill: new ol.style.Fill({
                    color: 'rgba(0, 0, 255, 0.1)'
                })
            });
            serviceResult.result.pathList.map(function (result) {
                //添加分析出的路线
                var routeSource = new ol.source.Vector({
                    features: (new ol.format.GeoJSON()).readFeatures(result.route)
                });
                var pathLayer = new ol.layer.Vector({
                    source: routeSource,
                    style: routeLayerStyle
                });
                map.addLayer(pathLayer);
                //添加分析出的引导点
                var guideSource = new ol.source.Vector({
                    features: (new ol.format.GeoJSON()).readFeatures(result.pathGuideItems)
                });
                var guideLayer = new ol.layer.Vector({
                    source: guideSource,
                    style: guideLayerStyle
                });
                map.addLayer(guideLayer);
            });
        });
    }
</script>
</body>
</html>